(function (G) {
    G.vue({
        data: {
            inventoryId:null,
            units: [], // 前一个页面传过来的所有机构
            updown: 1, // 控制隐藏和显示
            cameraimg: G.Storage.get('APIURL') + '/file/1594974455984164864', // 显示上传图标
            param: {}, // post参数
            description: null, // 详情描述
            img: [], // 上传的图片
            dispatchUser: null, // 指派用户
            resolveUser: null, // 分配人
            userNameList: [], // 所有用户名
            userList: [], // 所有用户
            index6: 0,
            urgency: 2, // 紧急程度
            source: 3, // 来源
            feedback: 3, // 监督员上报
            type: 1, // 案件类型
            firstType: 1, // 大类
            secondType: null, // 小类
            surplusTime: null, // 剩余处理时间
            applicant: null, // 申请人
            dispatchUserIDList: [], // 指派用户id，可能多个
            dispatchUserNameList: [], // 指派用户name，可能多个
            index1: 0, // 选择的第几个指派用户
            urgencyList: ['普通', '紧急'], // 紧急程度列表
            index2: 1, // 第几个紧急程度，默认紧急
            sourceList: ['12319', '城管通', '其他'], // 案件来源列表
            index3: 2, // 第几个案件来源，默认其他
            typeList: ['事件', '部件'], // 案件类型列表
            index4: 0, // 第几个案件类型
            firstTypeList: ['市容环境设施', '交通设施', '公共设施', '市容环境类', '宣传广告', '施工管理', '突发事件', '街面秩序'], // 大类列表
            index5: 0, // 第几个大类
        },
        methods: {
            // 删除图片
            delPic(e) {
                const {
                    index
                } = e.currentTarget.dataset

                let uploadList = [...this.data.img]
                uploadList.splice(index, 1)

                this.setData({
                    img: uploadList
                })

            },
            // 格式化时间
            formatDate(times) {
                var date = new Date(times);
                var year = date.getFullYear().toString(); //年份
                var month = (date.getMonth() + 1).toString(); //月份
                var day1 = date.getDate(); //日
                if (day1.toString().length == 1) {
                    var day = '0' + day1.toString()
                } else {
                    var day = day1.toString()
                }
                var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
                var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
                var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
            },
            // 点击提交按钮
            submit() {
                let that = this
                let params = that.data.param
                let editForm = {}
                let time1 = new Date()
                editForm.inventoryId = that.data.inventoryId
                editForm.location = params.location
                editForm.latitude = params.latitude
                editForm.longitude = params.longitude
                editForm.description = that.data.description
                editForm.img = that.data.img.toString()
                editForm.dispatchUser = that.data.dispatchUser
                editForm.dispatchTime = that.formatDate(time1)
                editForm.urgency = that.data.urgency
                editForm.source = that.data.source
                editForm.feedback = that.data.feedback
                editForm.type = that.data.type
                editForm.firstType = that.data.firstType
                editForm.secondType = that.data.secondType
                editForm.surplusTime = that.data.surplusTime
                editForm.applicant = that.data.applicant
                editForm.resolveUser = that.data.resolveUser ?? that.data.resolveUser
                editForm.status = that.data.dispatchUser == null ? 1 : 2
                editForm.applyType = that.data.dispatchUser == null ? 1 : 2
                G.post('/api/model/g_case', editForm).then(res => {
                    if (!res.errorMsg) {
                        let addID = res.id
                        params.caseId = addID
                        G.post('/v1/task/add', {
                            userId: G.Storage.get('userInfo').id,
                            flowId: that.data.dispatchUser == null ? '1588901038793035776' : '1589200662648983552',
                            data: editForm,
                            title: editForm.description+editForm.location,
                            toUsers: G.Storage.get('userInfo').id
                        }).then(res1 => {
                            if (!res1.errorMsg) {
                                G.put('/api/model/g_case', {
                                    id: addID,
                                    taskID: res1.task.id
                                }).then(res2 => {
                                    if (!res2.errorMsg) {
                                        wx.navigateBack()
                                        const eventChannel = this.getOpenerEventChannel()
                                        eventChannel.emit("someEvent", {
                                            id: addID
                                        })
                                        wx.showToast({
                                            title: '添加案件成功',
                                        })
                                    } else {
                                        wx.showToast({
                                            title: res2.errorMsg,
                                            icon: 'none'
                                        })
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: res1.errorMsg,
                                    icon: 'none'
                                })
                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.errorMsg,
                            icon: 'none'
                        })
                    }
                })
            },
            // 输入剩余时间
            inputSurplusTime(e) {
                this.setData({
                    surplusTime: e.detail.value
                })
                console.log(this.data.surplusTime);
            },
            // 选择小类
            inputSecondType(e) {
                this.setData({
                    secondType: e.detail.value
                })
                console.log(this.data.secondType);
            },
            // 选择大类
            selectFirstType(e) {
                this.setData({
                    index5: e.detail.value,
                    firstType: parseInt(e.detail.value) + 1
                })
                console.log(this.data.firstType);
            },
            // 选择类型
            selectType(e) {
                this.setData({
                    index4: e.detail.value,
                    type: parseInt(e.detail.value) + 1
                })
                console.log(this.data.type);
            },
            // 案件来源选择
            selectSource(e) {
                this.setData({
                    index3: e.detail.value,
                    source: parseInt(e.detail.value) + 1
                })
                console.log(this.data.source);
            },
            // 选择紧急程度
            selectUrgency(e) {
                this.setData({
                    index2: e.detail.value,
                    urgency: parseInt(e.detail.value) + 1
                })
                console.log(this.data.urgency);
            },
            selectDispatchUser(e) {
                let that = this
                console.log('-----e', e.detail.value);
                let uesr = that.data.dispatchUserIDList[e.detail.value]
                that.setData({
                    index1: e.detail.value,
                    dispatchUser: uesr == '不指定' ? null : uesr
                })
                console.log(that.data.dispatchUser);
            },
            selectResolvUser(e) {
                let that = this
                console.log('-----e', e.detail.value);
                let uesr = that.data.userList[e.detail.value].id
                that.setData({
                    index6: e.detail.value,
                    resolveUser: uesr == '不指定' ? null : uesr
                })
                console.log(that.data.resolveUser);
            },
            // 输入详情描述
            inputChage(e) {
                console.log('---', e.detail.value, G.Storage.get('userInfo')['id']);
                this.setData({
                    description: e.detail.value
                })
            },
            // 选择图片
            chooseMedia() {
                let that = this
                wx.chooseMedia({
                    count: 9,
                    mediaType: ['image'],
                    sourceType: ['album', 'camera'],
                    maxDuration: 30,
                    camera: 'back',
                    success(res) {
                        let list = that.data.img
                        for (let i in res.tempFiles) {
                            wx.uploadFile({
                                url: G.Storage.get('APIURL') + '/upload',
                                filePath: res.tempFiles[i].tempFilePath,
                                name: 'file',
                                header: {
                                    'token': G.Storage.get('userInfo').token
                                },
                                success(rej) {
                                    let url1 = G.Storage.get('APIURL') + '/file/' + JSON.parse(rej.data).id
                                    list.push(url1)
                                    that.setData({
                                        img: list
                                    })
                                },
                                fail(err) {
                                    console.log(err)
                                }
                            });
                        }
                    }
                })
            },
            // 微信图片调用upload
            uploadRequest(img) {
                wx.uploadFile({
                    url: G.Storage.get('APIURL') + '/upload',
                    filePath: img,
                    name: 'file',
                    header: {
                        'token': G.Storage.get('userInfo').token
                    },
                    success(rej) {
                        console.log('---rej.data', rej.data);
                        return JSON.parse(rej.data).id
                    },
                    fail(err) {
                        console.log(err)
                    }
                });
            },
            // 隐藏或者收起默认项
            updown() {
                let that = this
                that.setData({
                    updown: that.data.updown == 1 ? 2 : 1
                })
            },
            // 处理页面传参
            getInitParam(jsonp) {
                console.log('--jsonp', jsonp);
                let json1 = {}
                let that = this
                json1.latitude = jsonp.latitude
                json1.longitude = jsonp.longitude
                json1.location = jsonp.local
                json1.patrolDate = jsonp.patrolDate
                json1.userId = G.Storage.get('userInfo')['id']
                let users = []
                console.log(jsonp.unit);
                if (jsonp.unit.userId) {
                    users.push(jsonp.unit.userId)
                }
                // for (let i = 0; i < jsonp.units.length; i++) {
                //   if (jsonp.units[i].userId) {
                //     users.push(jsonp.units[i].userId)
                //   }
                // }
                that.setData({
                    param: json1,
                    units: jsonp.units,
                    inventoryId: jsonp.inventoryId,
                    dispatchUserIDList: users,
                    dispatchUser: users[0],
                    applicant: G.Storage.get('userInfo')['id'],
                })
                let user1 = [...users]
                let user2 = []
                for (let i = 0; i < user1.length; i++) {
                    G.get('/api/model/user', {
                        id: user1[i]
                    }).then(res => {
                        user2.push(res.name)
                        if (i == user1.length - 1) {
                            user2.push('不指定')
                            that.setData({
                                dispatchUserNameList: user2,
                            })
                        }
                    })
                }
                that.getResolveUser()
            },
            getResolveUser() {
                let that = this
                G.get('/api/model/user').then(res => {
                    if (!res.errorMsg) {
                        console.log('----res', res);
                        let userNameList = ['不指定']
                        res.forEach(i => {
                            userNameList.push(i.name)
                        })
                        that.setData({
                            userList: res,
                            userNameList: userNameList,
                        })
                    }
                })
            },



        },
        mounted(options) {
            if (options.param) {
                let jsonp = JSON.parse(options.param)
                this.getInitParam(jsonp)
            }
        }
    })
})(wx.$)